package com.acme.bankapp.domain.bank;

import com.acme.bankapp.exceptions.NotEnoughFundsException;

public abstract class AbstractAccount implements Account {
	private static final long serialVersionUID = 6692340695960047451L;
	
	static final String MES_NEGATIVE_BALANCE = "Balance must be positive or zero value";
	static final String MES_NEGATIVE_DEPOSIT = "Number of deposit must be positive value";
	static final String MES_TOO_SMALL_BALANCE = "Balance is less than required value";
	
	protected double balance;
	
	protected AbstractAccount(double balance) throws IllegalArgumentException {
		if (balance < 0)
			throw new IllegalArgumentException(MES_NEGATIVE_BALANCE);
		this.balance = balance;
	}
	
	public void deposit(double x) throws IllegalArgumentException {
		if (x < 0)
			throw new IllegalArgumentException(MES_NEGATIVE_DEPOSIT);
		
		this.balance += x;
	}
	
	public abstract void withDraw(double x) throws IllegalArgumentException, NotEnoughFundsException;	
	public abstract double maximumAmountToWithdraw();
	
	public double getBalance() {
		return balance;
	}
	
	public long decimalValue() {
		return Math.round(balance);
	}

	@Override
	public int hashCode() {
		final int prime = 31;
		int result = 1;
		long temp;
		temp = Double.doubleToLongBits(balance);
		result = prime * result + (int) (temp ^ (temp >>> 32));
		return result;
	}

	@Override
	public boolean equals(Object obj) {
		if (this == obj)
			return true;
		if (obj == null)
			return false;
		if (getClass() != obj.getClass())
			return false;
		AbstractAccount other = (AbstractAccount) obj;
		if (Double.doubleToLongBits(balance) != Double
				.doubleToLongBits(other.balance))
			return false;
		return true;
	}
}
